package ui;

import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import simulationEngine.SimulationEngine;
import ui.grafic.PhysNetworkExecutionGrafic;
import ui.observ.PhysicalNetworkObservableUnit;
import datatypes.GuiAction;
import datatypes.GuiActionType;
import datatypes.ScenarioDescription;

public class SimulationExecutionPhysNetworkCard extends JScrollPane implements Observer {

	private JSplitPane contentContainer = null;
	//left Component == Stack
	private JPanel leftComponent = new JPanel();
	//right Component == Grafics
	private PhysNetworkExecutionGrafic rightComponent = null;
	//simulationEngineID for identify Observable in Update
	private int SimulationEngineID = -1;
	
	
	public SimulationExecutionPhysNetworkCard (SimulationEngine engine){
		super();
		this.SimulationEngineID = engine.getID();
		this.setViewportView(this.getContentContainer(engine));
		PhysicalNetworkObservableUnit.getInstance(SimulationEngineID).addObserver(this);
	}
	
	private JSplitPane getContentContainer(SimulationEngine engine){
		if (contentContainer == null){
			contentContainer = new JSplitPane();
			contentContainer.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
			contentContainer.setLeftComponent(this.getLeftComponent());
			contentContainer.setRightComponent(this.getRightComponent(engine.getScenarioParameter()));
		}
		return contentContainer;
	}
	
	private JPanel getLeftComponent(){
		if (leftComponent== null){
			leftComponent = new JPanel();
		}
		return leftComponent;
	}
	
	private PhysNetworkExecutionGrafic getRightComponent(ScenarioDescription desc){
		if (rightComponent == null){
			rightComponent = new PhysNetworkExecutionGrafic(desc);
		}
		return rightComponent;
	}

	@Override
	public void update(Observable arg0, Object obj) {
		if (obj instanceof GuiAction ){
			switch (((GuiAction)obj).getType()){
			case NEW_NODE:
				this.getRightComponent(null).addNode(((GuiAction)obj).getHost());
				break;
			case DROP_NODE:
				this.getRightComponent(null).removeNode(((GuiAction)obj).getHost());
				break;
			case NEW_CONNECTION:
				this.getRightComponent(null).addEdge(((GuiAction)obj).getEdge());
				break;
			case DROP_CONNECTION:
				this.getRightComponent(null).removeEdge(((GuiAction)obj).getEdge());
				break;		
			}
		}
		
	}
}
